package com.huayi.website.controller;

import com.huayi.website.entity.EnumLanguage;
import com.huayi.website.entity.EnumPages;
import com.huayi.website.entity.NewsEntity;
import com.huayi.website.service.NewsService;
import com.huayi.website.service.PageContentService;
import com.huayi.website.service.PagesService;
import com.huayi.website.utils.PagingResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class NewController {
    @Autowired
    private PagesService pagesService;
    @Autowired
    private NewsService newsService;
    @Autowired
    private PageContentService pageContentService;

    @RequestMapping("/{language}/news")
    public String home(HttpServletRequest request, Model model, @PathVariable String language) {
        EnumLanguage languageEnum = EnumLanguage.getEnum(language);
        model.addAttribute("language",language);
        //获取页面信息，设置title
        var page = pagesService.getById(EnumPages.NEWS);
        if (languageEnum == EnumLanguage.CHINESE) {
            model.addAttribute("layout", "layout_zh");
            model.addAttribute("pageTitle", page.getTitleCn());
        }
        else {
            model.addAttribute("pageTitle", page.getTitleEn());
            model.addAttribute("layout", "layout");
        }
        Integer pageIndex = 1, pageSize = 6;
        if (!StringUtils.isEmpty(request.getParameter("page"))) {
            pageIndex = Integer.parseInt(request.getParameter("page"));
        }
        PagingResponse<NewsEntity> result =  newsService.getAllList(pageIndex,pageSize,null,1,languageEnum.getIndex());
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("totalSize", result.getTotalSize());
        model.addAttribute("lists", result.getList());

        PagingResponse<NewsEntity> result1 =  newsService.getAllList(pageIndex,pageSize,null,2,languageEnum.getIndex());
        model.addAttribute("pageIndex1", pageIndex);
        model.addAttribute("pageSize1", pageSize);
        model.addAttribute("totalSize1", result1.getTotalSize());
        model.addAttribute("lists1", result1.getList());
        return "news";
    }
    /**
     * 产品详情
     * @param request
     * @param language
     * @param newsId
     * @param model
     * @return
     */
    @RequestMapping("/{language}/newsDetail/{newsId}")
    public String home(HttpServletRequest request,  @PathVariable String language,@PathVariable Integer newsId,Model model){
        EnumLanguage languageEnum = EnumLanguage.getEnum(language);
        model.addAttribute("detail",newsService.getNewsById(newsId));

        model.addAttribute("pageTitle","Product Detail");
        if (languageEnum == EnumLanguage.CHINESE) {
            model.addAttribute("layout", "layout_zh");
        }
        else {
            model.addAttribute("layout", "layout");
        }
        model.addAttribute("menuId","profile");
        return "news_detail";
    }

    @RequestMapping("/{language}/aboutUs")
    public String aboutUs(HttpServletRequest request, Model model, @PathVariable String language) {
        EnumLanguage languageEnum = EnumLanguage.getEnum(language);
        model.addAttribute("language",language);
        model.addAttribute("menuId","about");
        //获取页面信息，设置title
        var page = pagesService.getById(EnumPages.NEWS);
        if (languageEnum == EnumLanguage.CHINESE) {
            model.addAttribute("layout", "layout_zh");
            model.addAttribute("pageTitle", page.getTitleCn());
        }
        else {
            model.addAttribute("pageTitle", page.getTitleEn());
            model.addAttribute("layout", "layout");
        }
        var pageContent = pageContentService.getByPageIdAndLanguage(EnumPages.Home.getIndex(), languageEnum);
        model.addAttribute("pc", pageContent);
        return "aboutUs";
    }
}
